package voip.ringer.client;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;

import cs252.voip.R;

import voip.capture.client.VoiceCaptureClient;
import voip.capture.server.VoiceCaptureServer;
import voip.directory.client.DirectoryClient;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class RingerClient extends Activity {
	Socket serverSocket;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
        this.setContentView(R.layout.in_call);

		
		this.connect();
	}
	
	public void connect() {
		Bundle extras = getIntent().getExtras();
		try {
			
			Log.d("MOOOOO", "NOT HERE");

			Intent VoiceCaptureService = new Intent(RingerClient.this, VoiceCaptureServer.class);
			
			VoiceCaptureService.putExtra("User_IP", extras.getString("User_IP"));
			startService(VoiceCaptureService);	//Service Started

			
			Log.d("RIGHT IN RINGER", extras.getString("User_IP"));

			
			
			Log.d("BAMMY", "Should be connecting to "  + extras.getString("User_IP"));
			InetAddress inet = InetAddress.getByName(extras.getString("User_IP"));
			serverSocket = new Socket();
			SocketAddress sa = new InetSocketAddress(inet, 4445);
			serverSocket.connect(sa);	//Connect (Giving IP address)
			serverSocket.close();
			
		
			
			//***************************//
			
			
			Intent VPCLient = new Intent(RingerClient.this, VoiceCaptureClient.class);
			//Pass along IP to the Client (They are going to connect to the Voice Capture Service
			VPCLient.putExtra("User_IP", extras.getString("User_IP"));
		    RingerClient.this.startActivity(VPCLient); //Launch the Voice Capture Client
		    

		    finish();
		//	serverSocket.close();	//Not needed
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
